﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CustomerMgr.Data.Models;

namespace CustomerMgr.Data
{
    public class ProductFactory
    {
        public static List<Product> SelectAll()
        {
            try
            {
                var db = new ordermanagerContext();
                return db.Products.ToList();
            }
            catch
            {
                return null;
            }
        }
        public static Product SelectOne(int productId)
        {
            try
            {
                var db = new ordermanagerContext();
                return db.Products.FirstOrDefault(s => s.Id.Equals(productId));
            }
            catch
            {
                return null;
            }
        }
        public static bool Insert(Product product)
        {
            try
            {
                var db = new ordermanagerContext();
                db.Products.Add(product);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool Update(Product product)
        {
            try
            {
                var db = new ordermanagerContext();
                var cus = (from i in db.Products
                           where i.Id == product.Id
                           select i).First();
                cus.Name = product.Name;
                cus.Description = product.Description;
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool Delete(Product product)
        {
            try
            {
                var db = new ordermanagerContext();
                var cus = (from i in db.Products
                           where i.Id == product.Id
                           select i).First();
                db.Products.Remove(cus);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
